package com.ruitu.health_2.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.ruitu.arad.base.base_list.BaseRefreshActivity;
import com.ruitu.arad.support.widget.progress.ProgressLayout;
import com.ruitu.health_2.R;
import com.ruitu.health_2.mvp.model.ArtManModel;
import com.ruitu.health_2.mvp.presenter.ArtManPresenter;
import com.ruitu.health_2.ui.adapter.ModelArtManListAdapter;
import com.ruitu.router_module.bean.PageModel2;
import com.ruitu.router_module.bean.product.ArtMan;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

// 艺人列表
public class ArtManListActivity<P, M> extends BaseRefreshActivity<ArtManPresenter.ArtManListP, ArtManModel.ArtManListM> {
    private RelativeLayout rl_empty;

    private List<ArtMan> artManList = new ArrayList<>();// 艺人列表
    private ModelArtManListAdapter adapter;

    private int currPage = 1;
    private int totalPage = 1;
    private String name = "";// 传过来的搜索关键字
    private int type = 0;// 传过来的艺人列表类型标识 1 所有读书会的艺人

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_art_man_list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rl_empty = findViewById(R.id.rl_empty);

        ((TextView) (rl_empty.findViewById(R.id.txt_description))).setText("暂时没有找到相关艺人");

        Bundle bundle = getIntent().getExtras();
        if (null != bundle) {
            name = bundle.getString("name");
            type = bundle.getInt("type");
        }

        setHeadTitle("艺人列表");

        reqDataList();
        showProgress();
    }

    protected void showOrHideEmptyView() {
        if (isDataEmpty(artManList)) {
            rl_empty.setVisibility(View.VISIBLE);
        } else {
            rl_empty.setVisibility(View.GONE);
        }
    }

    private void reqDataList() {
        if (type == 1) {// 所有读书会的艺人
            p.reqArtManListByName(20, currPage, name);// 需要修改
        } else {
            p.reqArtManListByName(20, currPage, name);
        }
    }

    @Override
    public void onReqComplete(int code, boolean isOk, Object data) {
        if (code == 1) {
            if (currPage == 1) {
                artManList.clear();
            }
            PageModel2<ArtMan> artManPage = (PageModel2<ArtMan>) data;
            totalPage = artManPage.getTotalPage();
            List<ArtMan> newData = artManPage.getList();
            artManList.addAll(newData);
            adapter.setDataList(artManList);

            showOrHideEmptyView();
        }
    }

    @Override
    protected RecyclerView.Adapter initAdapter() {
        adapter = new ModelArtManListAdapter(this);
        adapter.type = 1;// 单列样式
        return adapter;
    }

    @Override
    protected void onItemClick(View v, int position) {
        Bundle bundle = new Bundle();
        bundle.putString("id", artManList.get(position).getId());
        bundle.putString("name", artManList.get(position).getName());
        startActivity(ArtManDetailActivity.class, bundle);
    }

    @Override
    protected LRecyclerView findRecyclerView() {
        return findViewById(R.id.lrcv_list);
    }

    @Override
    protected void reqFirst() {
        currPage = 1;
        reqDataList();
    }

    @Override
    protected void reqMore() {
        currPage++;
        if (currPage <= totalPage) {
            reqDataList();
        } else {
            refreshNoMoreData();
        }
    }

    @Override
    protected ProgressLayout findProgressLayout() {
        return null;
    }
}
